﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waiter.Common;
using Waiter.Controler;
using Waiter.Report;

namespace Waiter.View
{
    public partial class FrmCaculateBill : DevComponents.DotNetBar.Office2007Form
    {
        public FrmCaculateBill()
        {
            InitializeComponent();
        }
        public void fsLoadCmbStaff()
        {
            StaffCtrl staffCtrl = new StaffCtrl();
            List<STAFF> staffList = staffCtrl.fsGetAllStaffs();
            if (staffList == null || staffList.Count == 0)
            {
                return;
            }

            List<ComboboxItem> itemList = (from s in staffList
                                           select new ComboboxItem()
                                           {
                                               Text = s.NAME,
                                               Value = s.CODE
                                           }).ToList();
            this.cmbStaff.ValueMember = CommonConstants.CMB_VALUE;
            this.cmbStaff.DisplayMember = CommonConstants.CMB_TEXT;
            this.cmbStaff.DataSource = itemList;

            CommonCtrl.fsCmbSetSelectedValue(this.cmbStaff, CommonUtils.fsGetOPID());
        }
        public FrmCaculateBill(string tableCode)
        {
            InitializeComponent();
            this.tableCode = tableCode;

            this.billItem = new BILL();
            billItem.CODE = CommonUtils.fsCreateNewCode();
            billItem.OPID = CommonUtils.fsGetOPID();
            billItem.ADD_START = DateTime.Now;
            billItem.UPD_START = DateTime.Now;
            billItem.CREATE_DATE = DateTime.Now;
            billItem.DISCOUNT = 0;
            billItem.FINAL_AMOUNT = 0;
            billItem.FINAL_BILL = true;
            billItem.TOTAL_AMOUNT = 0;
            billItem.STATUS_CODE = (int) BillStatus.USE;
            billItem.NOTE = "";
        }

        string tableCode = null;
        bool dataChangeFlag = CommonConstants.FLAG_OFF;
        BILL billItem = null;

        public BILL BillItem
        {
            get { return billItem; }
            set { billItem = value; }
        }

        public bool DataChangeFlag
        {
            get { return dataChangeFlag; }
            set { dataChangeFlag = value; }
        }

        private void FrmCaculateBill_Load(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmCaculateBill.FrmCaculateBill_Load", "", "");

            TableCtrl tblCtrl = new Controler.TableCtrl();
            TABLE tableItem = tblCtrl.fsFindTableByCode(this.tableCode);
            if (tableItem != null)
            {
                this.lblTableName.Text = "Bàn: " + tableItem.TABLE_NAME;
            }
            else
            {
                this.lblTableName.Text = "";
            }
            fsLoadDGOrderCoupon();
            fsLoadReportData();
            fsLoadCmbStaff();
        }

        private void fsLoadDGOrderCoupon()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmCaculateBill.fsLoadDGOrderCoupon", "", "");

            OrderCouponCtrl orderCouponCtrl = new OrderCouponCtrl();
            List<ORDER_COUPON_D> orderCouponList = orderCouponCtrl.fsGetCurrentOrderCouponList(tableCode);
            if (orderCouponList == null || orderCouponList.Count == 0)
            {
                return;
            }

            this.dgrOrderCoupon.Rows.Add(orderCouponList.Count);
            long money = 0;
            for (int i = 0; i < orderCouponList.Count; i++)
            {
                dgrOrderCoupon.Rows[i].Cells["colSTT"].Value = i + 1;
                dgrOrderCoupon.Rows[i].Cells["colName"].Value = orderCouponList[i].MENU.DRINKS_NAME;
                dgrOrderCoupon.Rows[i].Cells["colNum"].Value = orderCouponList[i].COUNT;
                dgrOrderCoupon.Rows[i].Cells["colPrice"].Value = orderCouponList[i].PRICE;
                dgrOrderCoupon.Rows[i].Cells["colMoney"].Value = CommonUtils.fsCreateMoneyString( orderCouponList[i].PRICE * orderCouponList[i].COUNT);
                money += orderCouponList[i].PRICE * orderCouponList[i].COUNT;
            }
            this.lblMoney.Text = CommonUtils.fsCreateMoneyString(money);

            this.BillItem.TOTAL_AMOUNT = money;
            this.BillItem.COUPON_CODE = orderCouponList[0].COUPON_CODE;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.dataChangeFlag = CommonConstants.FLAG_OFF;
            this.Close();
        }

        private void btnCaBill_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmCaculateBill.btnCaBill_Click", "", "");

            if(this.dgrOrderCoupon.Rows.Count == 0)
            {
                return;
            }

            // print bill
            if (fsPrintReport() != Common.ResultStatus.SUCCESS)
            {
                DialogResult dlrs = DevComponents.DotNetBar.MessageBoxEx.Show("Không in được hóa đơn, bạn có muốn tiếp tục không?",
                    CommonMessages.MSG_CONFIRM_TITLE, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dlrs != System.Windows.Forms.DialogResult.Yes)
                {
                    return;
                }
            }

            // create bill
            BillCtrl billCtrl = new BillCtrl();
            this.billItem.OPID = CommonCtrl.fsCmbGetSelectedValue(this.cmbStaff);
            if (billCtrl.fsInsertBill(this.BillItem) != Common.ResultStatus.SUCCESS)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Đã có lổi xảy ra, vui lòng thực hiện lại",
                    CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            OrderCouponCtrl orderCouponCtrl = new OrderCouponCtrl();
            orderCouponCtrl.fsUpdateStatus(this.billItem.COUPON_CODE, (int) OrderCouponStatus.WAS_CHARGED);

            this.dataChangeFlag = CommonConstants.FLAG_ON;
            this.Close();
        }

        private void fsLoadReportData()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmCaculateBill.fsLoadReportData", "", "");

            if (this.dgrOrderCoupon.Rows.Count == 0)
            {
                return;
            }

            ObjBillInfo billInfo = new ObjBillInfo();
            billInfo.CurrentDate = CommonUtils.fsFormatDate(CommonConstants.DDMMYYHHSS, DateTime.Now);
            billInfo.FacebookLink = Waiter.Properties.Settings.Default.FACEBOOK_lINK;
            billInfo.PhoneNumber = Waiter.Properties.Settings.Default.PHONE_NUMBER;
            billInfo.TableName = this.lblTableName.Text;
            billInfo.TotalMoney = this.lblMoney.Text;

            List<ObjBillDetailInfo> billDetailList = new List<ObjBillDetailInfo>();
            for (int i = 0; i < this.dgrOrderCoupon.Rows.Count; i++)
            {
                ObjBillDetailInfo billDetailItem = new ObjBillDetailInfo();
                billDetailItem.ProductName = this.dgrOrderCoupon.Rows[i].Cells["colName"].Value.ToString();
                billDetailItem.Count = this.dgrOrderCoupon.Rows[i].Cells["colNum"].Value.ToString();
                billDetailItem.Money = this.dgrOrderCoupon.Rows[i].Cells["colMoney"].Value.ToString();
                billDetailList.Add(billDetailItem);
            }
            this.ObjBillInfoBindingSource.DataSource = billInfo;
            this.ObjBillDetailInfoBindingSource.DataSource = billDetailList;
            this.reportViewer1.RefreshReport();
        }

        private ResultStatus fsPrintReport()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmCaculateBill.fsPrintReport", "", "");

            try
            {
                AutoPrintCtrl autoprintme = new AutoPrintCtrl(this.reportViewer1.LocalReport,
                    this.dgrOrderCoupon.Rows.Count);
                autoprintme.Print();
            }
            catch (Exception)
            {
                return Common.ResultStatus.ERROR;
            }
            return Common.ResultStatus.SUCCESS;
        }
    }
}
